const mongoose = require('./db');
const Schema = mongoose.Schema;

// 角色模型
const roleSchema = new Schema({
    name: { type: String, required: true, unique: true }, // 角色名称：老板、管理员、员工
    description: String, // 角色描述
    permissions: [String], // 权限列表
    createTime: {
        type: Date,
        default: Date.now
    }
});

// 用户模型
const userSchema = new Schema({
    username: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    email: { type: String, required: false },
    phone: String,
    sex: String,
    age: Number,
    role: { type: Schema.Types.ObjectId, ref: 'Role' }, // 关联角色
    store: { type: Schema.Types.ObjectId, ref: 'Store', required: false }, // 所属门店，可为空
    status: { type: String, enum: ['active', 'inactive'], default: 'active' }, // 账号状态，active为在职，inactive为离职
    createTime: {
        type: Date,
        default: Date.now
    }
});

// 门店模型
const storeSchema = new Schema({
    name: { type: String, required: true }, // 门店名称
    address: { type: String, required: true }, // 门店地址
    phone: { type: String, required: true }, // 联系电话
    manager: { type: String, required: true }, // 店长姓名
    status: { type: String, enum: ['营业中', '暂停营业', '装修中'], default: '营业中' }, // 门店状态
    staffCount: { type: Number, default: 0 }, // 员工数量
    businessHours: { type: String, default: '09:00-21:00' }, // 营业时间
    description: String, // 门店描述
    latitude: { type: Number }, // 纬度
    longitude: { type: Number }, // 经度
    createTime: {
        type: Date,
        default: Date.now
    },
    updateTime: {
        type: Date,
        default: Date.now
    }
});

// 更新时间中间件
storeSchema.pre('save', function(next) {
    this.updateTime = new Date();
    next();
});

const User = mongoose.model('User', userSchema);
const Role = mongoose.model('Role', roleSchema);
const Store = mongoose.model('Store', storeSchema);

module.exports = {
    User,
    Role,
    Store
};